package com.example.demo.proc.controller;

import com.example.demo.proc.domain.ProcDefinitionInfo;
import com.example.demo.proc.domain.ProcRuntimeInfo;
import com.example.demo.proc.domain.ProcTaskInfo;
import com.example.demo.proc.mapper.ProcDefinitionInfoMapper;
import com.example.demo.proc.mapper.ProcRuntimeInfoMapper;
import com.example.demo.proc.mapper.ProcTaskInfoMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.ContextValue;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Controller
@SchemaMapping(typeName = "ProcQuery")
@RequiredArgsConstructor
public class ProcQueryController {

  private final ProcTaskInfoMapper procTaskInfoMapper;
  private final ProcRuntimeInfoMapper procRuntimeInfoMapper;
  private final ProcDefinitionInfoMapper procDefinitionInfoMapper;

  @QueryMapping("proc")
  public Map<String, Object> getProc() {
    return new HashMap() {
      {
        put("classname", ProcQueryController.class.getName());
        put("name", "ProcQuery");
      }
    };
  }

  @SchemaMapping("task")
  public ProcTaskInfo getProcTaskInfo(@Argument String taskId) {
    return procTaskInfoMapper.selectById(taskId);
  }

  @SchemaMapping(typeName = "ProcTaskInfo", value = "prevTask")
  public ProcTaskInfo getProcTaskInfoOfProcTaskInfoPrev(ProcTaskInfo task) {
    return procTaskInfoMapper.selectById(task.getPrevTaskId());
  }

  @SchemaMapping(typeName = "ProcTaskInfo", value = "runtime")
  public ProcRuntimeInfo getTaskOfRunTime(ProcTaskInfo task) {
    return procRuntimeInfoMapper.selectById(task.getProcRuId());
  }

  @SchemaMapping(typeName = "ProcTaskInfo", value = "definition")
  public ProcDefinitionInfo getTaskOfDefinition(ProcTaskInfo task) {
    return procDefinitionInfoMapper.selectById(task.getProcDeId());
  }

  @SchemaMapping("runtime")
  public ProcRuntimeInfo getProcRuntimeInfo(@Argument String runId) {
    return procRuntimeInfoMapper.selectById(runId);
  }

  @SchemaMapping(typeName = "ProcRuntimeInfo", value = "definition")
  public ProcDefinitionInfo getProcRuntimeInfoOfDefinition(ProcRuntimeInfo runtimeInfo) {
    return procDefinitionInfoMapper.selectById(runtimeInfo.getProcDeId());
  }

  @SchemaMapping("definition")
  public ProcDefinitionInfo getProcDefinitionInfo(@Argument String defId) {
    return procDefinitionInfoMapper.selectById(defId);
  }
}
